<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="mask"></div>
<div class="upon-mask">
    <h3 style="margin-top: 0">
        <span>用户资料</span>
        <span onclick="closeMask();" class="close-mask">×</span>
    </h3>
    <div class="container" style="width: 100%">
        <div class="row">
            <div class="col-md-9">
                <div class="row">
                    <div class="col-md-3">
                        <img id="mask_userAvatar" src="">
                    </div>
                    <div class="col-md-9">
                        <div id="mask_userName" style="padding-top: 5px;font-size: 26px"></div>
                        <div id="mask_email" style="font-size: 18px"></div>
                    </div>
                </div>
                <div style="height: 10px;"></div>
                <div style="font-size: 18px;">角色：<span id="mask_role"></span></div>
                <div style="font-size: 18px;">加入时间：<span id="mask_joinTime"></span></div>
                <div style="font-size: 18px;">简介：<span id="mask_simpleDesc"></span></div>
                <div style="height: 10px;"></div>
                <div class="row">
                    <div class="col-md-6" style="font-size: 18px;">
                        职业：<span id="mask_career"></span>
                    </div>
                    <div class="col-md-6" style="font-size: 18px;">
                        地址：<span id="mask_address"></span>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12" style="font-size: 18px;">
                        自我介绍：<span id="mask_full_desc"></span>
                    </div>
                </div>
                <div style="font-size: 18px;">空间访问量：<span id="mask_scanedCount"></span> 次</div>

            </div>
            <div class="col-md-3">
                <button id="mask_upRole" style="clear: both;margin: 10px;display: none;" type="button">升职</button>
                <button id="mask_downRole" style="clear: both;margin: 10px;display: none;" type="button">撤职</button>
                <button id="mask_deleteUser" style="clear: both;margin: 10px;display: none;" type="button">删除</button>
                <button id="mask_activateUser" style="clear: both;margin: 10px;display: none;" type="button">激活</button>
                <button id="mask_sendBlackHouse" style="clear: both;margin: 10px;display: none;" type="button">禁闭</button>
            </div>
        </div>

    </div>

</div>
<script>
    var mask = $(".mask");
    var maskView = $(".upon-mask");
    var buttonUpRole = $("#mask_upRole");
    var buttonDownRole = $("#mask_downRole");
    var buttonDeleteUser = $("#mask_deleteUser");
    var buttonActivateUser = $("#mask_activateUser");
    var buttonSendBlackHouse = $("#mask_sendBlackHouse");
    var myRole = $.cookie('role');
    var myId = $.cookie('id');
    var userId;
    var userName;
    var userRole;

    // 根据权限不同，按钮显示状态不同
    function showButtons() {
        if (parseInt(myRole) === 0 && parseInt(userRole) === 0) {
            buttonUpRole.hide();
            buttonDownRole.hide();
            buttonDeleteUser.hide();
            buttonActivateUser.hide();
            buttonSendBlackHouse.hide();
        } else if (parseInt(myRole) === 0 && parseInt(userRole) === 1) {
            buttonUpRole.show();
            buttonDownRole.show();
            buttonDeleteUser.show();
            buttonActivateUser.hide();
            buttonSendBlackHouse.show();
        } else if (parseInt(myRole) === 0 && parseInt(userRole) === 2) {
            buttonUpRole.show();
            buttonDownRole.hide();
            buttonDeleteUser.show();
            buttonActivateUser.show();
            buttonSendBlackHouse.show();
        } else if (parseInt(myRole) === 1 && parseInt(userRole) === 0) {
            buttonUpRole.hide();
            buttonDownRole.hide();
            buttonDeleteUser.hide();
            buttonActivateUser.hide();
            buttonSendBlackHouse.hide();
        } else if (parseInt(myRole) === 1 && parseInt(userRole) === 1) {
            buttonUpRole.hide();
            buttonDownRole.hide();
            buttonDeleteUser.hide();
            buttonActivateUser.hide();
            buttonSendBlackHouse.hide();
        } else if (parseInt(myRole) === 1 && parseInt(userRole) === 2){
            buttonUpRole.hide();
            buttonDownRole.hide();
            buttonDeleteUser.show();
            buttonActivateUser.show();
            buttonSendBlackHouse.show();
        }
    }

    function openMask(id) {
        mask.show();
        maskView.show();
        $.post(
            "${pageContext.request.contextPath}/backdoor/userPage", //url地址
            {"userId": id}, //请求参数
            function (user) { //执行成功后的回调函数
                $("#mask_userAvatar").attr("src", user.avatarUrl);
                $("#mask_userName").text(user.name);
                $("#mask_email").text(user.email);
                $("#mask_role").text(CON_ROLE[user.role]);
                var joinTime = computeDifferTime(user.time);
                $("#mask_joinTime").text(joinTime);
                $("#mask_simpleDesc").text(user.simDesc);
                $("#mask_career").text(user.career);
                $("#mask_address").text(user.address);
                $("#mask_full_desc").text(user.fullDesc);
                $("#mask_scanedCount").text(user.scannedCount);
                userRole = user.role;
                userId = user.id;
                userName = user.name;
                showButtons();
                //如果已激活，则不显示激活按钮
                if (parseInt(user.activateStatus) !== 0) {
                    buttonActivateUser.hide();
                }
            },
            "json"
        );
    }

    function closeMask() {
        mask.hide();
        maskView.hide();
        buttonUpRole.hide();
        buttonDownRole.hide();
        buttonDeleteUser.hide();
        buttonActivateUser.hide();
        buttonSendBlackHouse.hide();
    }

    //计算已过时间
    function computeDifferTime(startTime) {
        var differTime = new Date().getTime() - parseInt(startTime);
        var day = Math.floor(differTime / (24 * 60 * 60 * 1000));
        if (day > 0) return day + '天前';
        var hours = Math.floor(differTime / (60 * 60 * 1000));
        if (hours > 0) return hours + '小时前';
        var minutes = Math.floor(differTime / (60 * 1000));
        if (minutes > 0) return minutes + '分钟前';
        var seconds = Math.floor(differTime / 1000);
        return seconds + '秒前';
    }

    //五个按钮功能
    buttonUpRole.click(function () {
        if (parseInt(myRole) === 0 && parseInt(userRole) === 1) {
            if (confirm("超级管理员只能有一人，确定将超级管理员职位转让给 " + userName + " 吗？确认的话，你将以普通用户身份返回登陆页。")) {
                //执行转让超级管理员的操作；
                window.location.href = "${pageContext.request.contextPath}/backdoor/userPage/transfer?myId=" + myId + "&toId=" + userId;
            }
        } else if (parseInt(myRole) === 0 && parseInt(userRole) === 2) {
            if (confirm("确定将 " + userName + " 设为管理员吗？")) {
                //执行设置管理员的操作；
                window.location.href = "${pageContext.request.contextPath}/backdoor/userPage/setManager?userId=" + userId;
            }
        }
    });
    buttonDownRole.click(function () {
        if (confirm("确定将 " + userName + " 设为普通用户吗？")) {
            //执行设置普通用户的操作；
            window.location.href = "${pageContext.request.contextPath}/backdoor/userPage/setUser?userId=" + userId;
        }
    });
    buttonDeleteUser.click(function () {
        if (confirm("确定将 " + userName + " 从系统中删除吗？")) {
            //执行删除用户的操作；
            window.location.href = "${pageContext.request.contextPath}/backdoor/userPage/deleteUser?userId=" + userId;
        }
    });
    buttonActivateUser.click(function () {
        if (confirm("确定辅助 " + userName + " 激活吗？这样的话，该用户不再需要邮箱验证激活,密码找回功能将失效。")) {
            //执行激活用户的操作；
            window.location.href = "${pageContext.request.contextPath}/backdoor/userPage/activateUser?userId=" + userId;
        }
    });
    buttonSendBlackHouse.click(function () {
        var hour = prompt("填写禁闭时间(小时)：");
        if (hour != null) {
            if (hour > 0) {
                window.location.href = "${pageContext.request.contextPath}/backdoor/userPage/sendBlackHouse?userId=" + userId + "&hour=" + hour;
            } else {
                alert("输入不合法");
            }
        }
    });
    
</script>
